package edu.pku.sei.transformation.qvt.engine.mediniqvt;

import java.io.OutputStream;

import org.eclipse.ui.console.MessageConsole;

import edu.pku.sei.transformation.codegen.CodegenRegister;
import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.ModelType;
import edu.pku.sei.transformation.editor.model.TypedModel;
import edu.pku.sei.transformation.editor.model.execution.Pin;
import edu.pku.sei.transformation.editor.model.execution.TransformationExecution;
import edu.pku.sei.transformation.editor.views.console.ConsoleViewManager;
import edu.pku.sei.transformation.engine.EngineExtraData;
import edu.pku.sei.transformation.engine.IEngineAdapter;
import edu.pku.sei.transformation.engine.IEngine;
import edu.pku.sei.transformation.execution.ExecutionEnvironment;
import edu.pku.sei.transformation.qvt.action.MediniQVTCodegenAdapter;

public class MediniQVTAdapter implements IEngineAdapter {

	@Override
	public EngineExtraData getExtraData(EngineExtraData oringin) {
		return null;
	}

	@Override
	public String getID() {
		return "MediniQVT";
	}


	@Override
	public IEngine getRunner(TransformationExecution execution,
			ExecutionEnvironment env) throws Exception {
		MessageConsole console = ConsoleViewManager.getConsole(execution.getName());
		OutputStream stream = console.newMessageStream();
		MediniQVTRunner runner = new MediniQVTRunner(stream);
		runner.direction = execution.getDirection().getName();
		runner.transformation = execution.getTransformation().getName();
		
		AtomicTransformation trans = (AtomicTransformation) execution.getTransformation();
		for (TypedModel typedModel : trans.getTypedModel()) {
			if (typedModel.getType() instanceof ModelType) {
				ModelType modelType = (ModelType) typedModel.getType();
				runner.metaModelPath.add(modelType.getMetaModel().getFilePath());
			}
			Pin p = execution.getActualParameterByFormalParameter(typedModel);
			runner.modelPath.add(env.getPath(p));
			if (typedModel==execution.getDirection())
				runner.targetPath = env.getPath(p);
		}
		
		CodegenRegister.INSTANCE.generateCodeIfNotExist(MediniQVTCodegenAdapter.LANGUAGE, execution);
		runner.transformationPath = execution.getCode(MediniQVTCodegenAdapter.LANGUAGE).getAbsolutePath();
		
		return runner;
	}

}
